package com.hyc.myvideo.ui.base;


import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.acmenxd.toaster.Toaster;
import com.hyc.myvideo.AppContext;
import com.hyc.myvideo.data.api.Api;
import com.hyc.myvideo.data.api.model.UserInfo;
import com.hyc.myvideo.ui.service.AudioService;
import com.hyc.network.exception.ApiException;
import com.hyc.network.listener.HttpOnNextListener;

import butterknife.ButterKnife;
import timber.log.Timber;

/**
 * Created by laucherish on 16/3/15.
 */
public abstract class BaseFragment extends Fragment implements HttpOnNextListener {
    public Resources res;/*获取资源*/
    public Display currDisplay;
    /**全局应用*/
    protected AppContext appContext;
    /**全局接口调用*/
    protected Api api;
    /**全局用户信息*/
    public UserInfo userInfo;

    public AudioService audioService;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(getLayoutId(), container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, view);
        currDisplay = getActivity().getWindowManager().getDefaultDisplay();
        res = getResources();
        api=new Api(this,getActivity());
        this.appContext = AppContext.get(getActivity());
        userInfo = appContext.getUserInfo();
        audioService=appContext.getAudioService();
        initView();
        initData();
    }

    @Override
    public void onResume() {
        super.onResume();
        userInfo = appContext.getUserInfo();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        api=null;
    }

    /**
     * 从资源获取字符串
     */
    public String getStr(int resId) {
        return res.getString(resId);
    }

    @Override
    public void onDetach(){
        super.onDetach();
    }

    protected abstract int getLayoutId();
    protected  abstract void initView();
    protected  abstract void initData();
    protected abstract void onNexts(String result,String method);
    protected abstract void onError();

    @Override public void onNext(String s, String s1) {
        Timber.i("============onNext=================" +s);
        onNexts(s,s1);
    }

    @Override public void onError(ApiException e, String s) {
        onError();
        Toaster.show("无网络连接!");
    }
}
